package com.xiaoy.core.basic.security.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.xiaoy.util.store.config.ConfigTable;


@Entity
@Table(name = ConfigTable.Basic.SECURITY_ROLE)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) 
@SequenceGenerator(sequenceName = ConfigTable.Basic.SECURITY_ROLE+"_SEQ", name = ConfigTable.Basic.SECURITY_ROLE+"_SEQ", allocationSize = 1)
public class Role {
	
	private String name;
	private long priority;												// 优先级，作用于权限
	private RoleType type;												// 角色类型
	private List<PartyRole> partyRoles = new ArrayList<PartyRole>();	// 角色和组织机构的关联表
	private long sort;													// 排序
	private Role parent;												// 上级
	private List<Role>children = new ArrayList<Role>();					// 下级
	private long id;
	private String createuser;											// 创建人
	private Date createtime;											// 创建时间
	
	
	public Role () {}

	public Role (long id) {
		this.id = id;
	}
	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = ConfigTable.Basic.SECURITY_ROLE+"_SEQ")
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public long getPriority() {
		return priority;
	}

	public void setPriority(long priority) {
		this.priority = priority;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "typeid")
	public RoleType getType() {
		return type;
	}

	public void setType(RoleType type) {
		this.type = type;
	}

	@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "role")
	@Fetch(FetchMode.SUBSELECT)
	public List<PartyRole> getPartyRoles() {
		return partyRoles;
	}
	
	public void setPartyRoles(List<PartyRole> partyRoles) {
		this.partyRoles = partyRoles;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "pid")
	public Role getParent() {
		return parent;
	}

	public void setParent(Role parent) {
		this.parent = parent;
	}

	@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "parent")
	@Fetch(FetchMode.SUBSELECT)
	public List<Role> getChildren() {
		return children;
	}

	public void setChildren(List<Role> children) {
		this.children = children;
	}

	public long getSort() {
		return sort;
	}

	public void setSort(long sort) {
		this.sort = sort;
	}

	public String getCreateuser() {
		return createuser;
	}

	public void setCreateuser(String createuser) {
		this.createuser = createuser;
	}

	public Date getCreatetime() {
		return createtime;
	}

	public void setCreatetime(Date createtime) {
		this.createtime = createtime;
	}
	
}
